#coding=utf-8
from pyExcelerator import *
from parser import *
import time 

data = parse_xls('products.xls')[0][1]
max_num = max([i[0] for i in data.keys()])

wb = Workbook()
sheet = wb.add_sheet('products')

def spec2string(specs):
    s = ''
    for spec in specs:
        k, v = spec
        if k:
            s = s + ';\n' + k + ':' + v 
        else:
            s = s + ',' + v
    return s[2:]

for i in range(max_num):
    try:
        link = data[(i, 2)]
    except Exception, e:
        print i
        print e
        continue
    else:
        link = link.replace('\t', '')
        
        soup = get_soup(link)
        try:
            desc = get_description(soup)
            spec = spec2string( get_spec(soup) )
        except Exception, e:
            print i, data[(i, 0)]
            print link
            print e, '\n'
            continue
        else:
            print i, data[(i, 0)]
            
            sheet.write(i, 0, data[(i, 0)])
            sheet.write(i, 1, data[(i, 1)])
            sheet.write(i, 2, data[(i, 2)])
            sheet.write(i, 3, desc)
            sheet.write(i, 4, spec)
            #time.sleep(0.1)
    
wb.save('./products3.xls')
